/* 
 * Modbus app for RTU.
 * Copyright (c) 2012  apleilx
 * All rights reserved.
 * no limite
 */

/* ----------------------- Platform includes --------------------------------*/
#define _MBAPP_MODULE_
#include "mbapp.h"
#include "osObjects.h"                                          // RTOS object definitions



/*****************************************************************************//*!
* @brief   	Slave device handle.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/    
void ModbusTask (void  *p_arg)
{
    OS_ERR      p_err;
    CPU_TS      p_ts;
    
    OS_FLAGS    EventFlag;
        
    OSTaskSemSet(&ModbusTaskTCB, 0, &p_err);
    mbxInit(RTU_SLAVE, 9600, MB_PAR_EVEN);
    mbxObj.SlaveId=2;
    
    for(;;){
        
        EventFlag = OSFlagPend (&FlagModbusEvent,MBAPP_EVENT_MSK,500,OS_OPT_PEND_FLAG_SET_ANY,&p_ts, &p_err);
        
        if(mbxObj.RunSt.bits.MasterMode){

        } else {
            if(EventFlag & (MBAPP_COM1_EVENT_RTUEND | MBAPP_COM1_EVENT_TIMEOUT)){
                OSFlagPost (&FlagModbusEvent, MBAPP_COM1_EVENT_RTUEND|MBAPP_COM1_EVENT_TIMEOUT, OS_OPT_POST_FLAG_CLR, &p_err);
                
                if(EventFlag & MBAPP_COM1_EVENT_TIMEOUT) 
                    mbxObj.RcvSt = MB_RX_TIMEOUT;
                else
                    printf(" COM1 modbus event: RTU received !\n");
                
                mbSlavePoll(&mbxObj);
            }
            
            if(EventFlag & (MBAPP_COM2_EVENT_RTUEND | MBAPP_COM2_EVENT_TIMEOUT)){
            }
            
            if(EventFlag & (MBAPP_COM3_EVENT_RTUEND | MBAPP_COM3_EVENT_TIMEOUT)){
            }
        }
    }
}




